//
//  MainViewController.swift
//  weibosina
//
//  Created by 李明禄 on 15/10/8.
//  Copyright © 2015年 李明禄. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        addChildViewControllers()
        
    }
    
    

    
    /**
    设置按钮
    */
    func setupComposeButton() {
        print(composeButton)
        let plusButton = composeButton
        
        //计算宽度,这里使用的是控制器的count,而不是tabbar的count
//        let width = tabBar.bounds.width / CGFloat(tabBar.subviews.count)
        
        let width = tabBar.bounds.width / CGFloat(viewControllers!.count)
        
        let height = tabBar.bounds.height
        
        let rect = CGRectMake(0, 0, width, height)
        
        
        plusButton.frame = CGRectOffset(rect, 2 * width, 0)
        
    }
    
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        setupComposeButton()
        
    }
    
    
    
///  点击按钮功能实现
    func clickComposeButton() {
        print("\(__FUNCTION__)")
    }
    


    ///   添加子控制器

    private func addChildViewControllers() {

       addChildViewController(HomeTableViewController(), vcName: "首页", imageName: "tabbar_home")
        

        addChildViewController(MessageTableViewController(), vcName: "消息", imageName: "tabbar_message_center")
        
        
        addChildViewController(UIViewController())
        

        addChildViewController(DiscoverTableViewController(), vcName: "发现", imageName: "tabbar_discover")
        
       
        addChildViewController(ProfileTableViewController(), vcName: "我", imageName: "tabbar_profile")
    }

    
    
    /**
    添加控制器函数
    
    - parameter vc:        控制器
    - parameter vcName:    控制器名称
    - parameter imageName: tabbar图像名称
    */
    
    //private的是类的内部参数,外部不可以访问

    private func addChildViewController(vc:UIViewController, vcName: String, imageName: String) {
    
        //设置标题
        vc.title = vcName
        
         //设置tabbar图像
        vc.tabBarItem.image = UIImage(named: imageName)
        
        //添加导航控制器
        let nav = UINavigationController(rootViewController: vc)
        
        //将导航控制器添加到tabbar控制器中
        addChildViewController(nav)
       
        
    }

    
    //MARK:懒加载button
    
    lazy private var composeButton: UIButton = {
//        let button = UIButton(type: UIButtonType.ContactAdd)
        
        let button = UIButton()
        
        //添加按钮图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //将按钮添加到tabbar中
        self.tabBar.addSubview(button)

        
        button.addTarget(self, action: "clickComposeButton", forControlEvents: UIControlEvents.TouchUpInside)
                
        return button
    }()
    
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

}
